\subsection*{Exercise 1 solution}

The solution for the exercise is as follows
%
\lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting{./src/all_in_one/bn-allinone.cpp}
\lstset{basicstyle=\sf}

First of all, we have defined a user type called \cpp{real}. If we want to
change the precision for the calculations, it will be sufficient to just change
the definition for the \cpp{real} type. All the code would be updated
coherently.

In order to have the possibility to pass an arbitrary function to the method
that performs the evaluation of the zero we use a \emph{function pointer}. The
instruction
\begin{lstlisting}
typedef real (*fctptr)(real);
\end{lstlisting}
defines a type \cpp{fctptr} as a pointer to a function that have a \cpp{real} as
argument and thta have a \cpp{real} as return value.

we can adopt different strategies to stop the iterations of a method that
computes a zero. When the function derivative absolute value is close to $1$ in
the neighborhood of the zero an efficient way to set a stop criterion is by
using the \emph{residual}. More specifically, we consider that the method as
converged at the iteration $k$ when $\module{\function{f}{x\iter{k}}} <
\cpp{tol}$.

An alternative way, based on a check on the \emph{increment}, is instead
suitable for fixed point iterations when the derivative of the iteration
function $\function{\phi}{\cdot}$ is far away from $1$ in modulus near the zero.
The criterion stops the iterations when $\module{x\iter{k+1} - x\iter{k}} <
\cpp{tol}$. For the Newton's method, where $\function{\phi'}{\alpha} = 0$ by
definition, the check on the increment is a typical choice. The implementation
takes care of the different types of stop criteria with an \emph{enumeration},
called \cpp{checkT}. Every method has a member \cpp{M\_check} of type
\cpp{checkT} that chooses the convergence criterion.

If we compile with
\begin{verbatim}
    g++ -o bn-allinone bn-allinone.cpp -Wall
\end{verbatim}
and execute the resulting program we get
\begin{verbatim}
0.707107        27
0.707107        7
0.707107        14 1
\end{verbatim}

